package com.masterclient.app.businessmodule.homemodule.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.widget.PicassoImageLoader;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.modules.income.model.HomeDataBean;
import com.masterclient.app.utils.CommonUtil;
import com.masterclient.app.widget.UIndicator;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import static com.eoner.baselibrary.utils.ScreenUtils.dp2px;

public class BannerAdapter extends DelegateAdapter.Adapter<HomeViewHolder>  {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private List<HomeDataBean.HomeAdsItem> bannerBean;
    private EventAdDataCallback eventAdDataCallback;
    private  Banner advBanner;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public interface ColorCallBack{
        void color_change(int color);
    }

    public BannerAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, List<HomeDataBean.HomeAdsItem> bannerBean) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.bannerBean = bannerBean;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    public void stopAutoPlay() {
        if (advBanner != null) {
            advBanner.stopAutoPlay();
        }
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View item = holder.itemView.findViewById(R.id.item);
        advBanner = holder.itemView.findViewById(R.id.advBanner);
        UIndicator uIndicator = holder.itemView.findViewById(R.id.indicator1);

        if(bannerBean != null){
            List<HomeDataBean.HomeAdsItem> banners = bannerBean;
            List<String> imgUrls = new ArrayList<String>();
            if (banners != null) {
                for (HomeDataBean.HomeAdsItem shBannerBean : banners) {
                    imgUrls.add(shBannerBean.banner);
                }

                advBanner.setOffscreenPageLimit(imgUrls.size());
                if(imgUrls.size() > 0){
                    uIndicator.attachToViewPager(imgUrls.size());
                    uIndicator.onViewPageSelected(0);
                }
                //设置banner样式
                advBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                //设置图片加载器
                advBanner.setImageLoader(new PicassoImageLoader(ScreenUtils.getScreenWidth(), ScreenUtils.dp2px(210)));
                //设置图片集合
                advBanner.setImages(imgUrls);
                //设置banner动画效果
                advBanner.setBannerAnimation(Transformer.Stack);
                //设置自动轮播，默认为true
                advBanner.isAutoPlay(true);
                //设置轮播时间
                advBanner.setDelayTime(5000);
                //设置指示器位置（当banner模式中有指示器时）
                advBanner.setIndicatorGravity(BannerConfig.CENTER);
                //banner设置方法全部调用完毕时最后调用
                advBanner.start();
                advBanner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        HomeDataBean.HomeAdsItem banner = bannerBean.get(position);
                        CommonUtil.goAnyWhere(mContext,banner.targetUrl,"","","","","");

                    }
                });
                advBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        advBanner.setVisibility(View.VISIBLE);
                        int cur = position + 1;

                        if (uIndicator != null){
                            uIndicator.onViewPageSelected(position);
                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            }
        }
        item.setPadding(0,0,0,0);
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 2;
    }

}
